/*** Eclipse Class Decompiler plugin, copyright (c) 2016 Chen Chao (cnfree2000@hotmail.com) ***/
package shop.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Vector;
import shop.dao.BaseDao;

public class CartBean extends BaseDao {
	public String p_id;
	public float p_price;
	public String p_image;
	public int p_number;
	private Connection conn = null;
	private PreparedStatement pstmt = null;
	private ResultSet rs = null;
	private Vector cart = null;

	public Vector addCart(String p_id, Vector cart) {
		this.cart = cart;
		String sql = "select p_name,p_price,p_image from Product where p_id=\'" + p_id + "\'";
		float p_price = 0.0F;
		String p_image = "";

		try {
			this.conn = this.getConn();
			this.pstmt = this.conn.prepareStatement(sql);
			this.rs = this.pstmt.executeQuery();
			if (this.rs.next()) {
				p_price = this.rs.getFloat("p_price");
				p_image = this.rs.getString("p_image");
			}
		} catch (Exception arg8) {
			;
		}

		this.p_id = p_id;
		this.p_price = p_price;
		this.p_image = p_image;
		this.p_number = 1;
		boolean Flag = true;
		if (cart == null) {
			cart = new Vector();
		} else {
			for (int i = 0; i < cart.size(); ++i) {
				CartBean goodsitem = (CartBean) cart.elementAt(i);
				if (goodsitem.p_id.equals(this.p_id)) {
					++goodsitem.p_number;
					cart.setElementAt(goodsitem, i);
					Flag = false;
				}
			}
		}

		if (Flag) {
			cart.addElement(this);
		}

		return cart;
	}

	public int deleteCart(int p_id, Vector cart) {
		this.cart = cart;
		if (cart == null) {
			return 0;
		} else {
			cart.removeElementAt(p_id);
			return 1;
		}
	}

	public CartBean updateCart(Vector cart, int i, String num) {
		this.cart = cart;
		CartBean goodsitem = (CartBean) cart.elementAt(i);
		if (num != null && num != "") {
			goodsitem.p_number = Integer.parseInt(num);
		}

		return goodsitem;
	}
}